<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Share extends FOX_Controller {

    function __construct ()
    {

        parent::__construct();
        $this->load->model('shareGoodRecord_m');
        $this->load->model('good_m');
        $this->load->model('goodVouchers_m');
        $this->load->model('freeVouchers_m');
        if(!$this->session->userdata('user_id')){
            redirect(site_url('nologin'));
        }
    }

    /**
     * 分享记录入库
     */
    public function record_share(){

        $data['goods_id'] = !empty($this->input->post('good_id')) ?$this->input->post('good_id') : false;
        $data['user_id'] = !empty($this->input->post('user_id')) ?$this->input->post('user_id') : false;
        $record = $this->shareGoodRecord_m->get_share_record_limit($data['user_id'],$data['goods_id']);
        if(empty($record)){
            $data['ctime'] = date("Y-m-d H:i:s",time());
            $this->shareGoodRecord_m->add_share_record($data);
        }
    }

    public function get_voucher(){
        $data['good_id'] = !empty($this->input->get('good_id')) ?$this->input->get('good_id') : false;
        $data['user_id'] = !empty($this->input->get('user_id')) ?$this->input->get('user_id') : false;
        // 根据商品ID获取商家ID
        $good_info = $this->good_m->get_shop_goods_by_id($data['good_id']);
        $data['shop_id'] = $good_info['shop_id'];
        $data['voucher_time'] = time();
        // 免单券失效时间
        $data['voucher_end_time'] = time()+$good_info['good_voucher_day']*24*60*60;
        // 判断是否满足领券条件
        $is_can_voucher = $this->is_can_voucher($data['good_id'],$data['user_id']);
        if($is_can_voucher){
            //领取
            $res = $this->freeVouchers_m->add_free_vouchers($data);
            if($res){
                // 免单券数量减一
                $this->db->set('good_voucher_num','good_voucher_num-1',false)->where('good_id',$data['good_id'])->update('shop_goods');
                echo json_encode(['status'=>2,'msg'=>"领取成功"]);exit();
            }
        }else{
            // 不能领取
            echo json_encode(['status'=>404,'msg'=>"领取失败"]);exit();
        }

    }

    private function is_can_voucher($goods_id,$user_id)
    {
        $share = $this->shareGoodRecord_m->get_share_record_limit($user_id,$goods_id);
        // 免单券规则
        $vouchers = $this->goodVouchers_m->get_good_vouchers_by($goods_id);
        if (!empty($vouchers)) {
            foreach ($vouchers as $key2 => $val2) {
                $vouchers_id_arr[] = $val2['voucher_type'];
                $vouchers_kv[$val2['voucher_type']] = $val2['voucher_tnum'];
            }
            $record['voucher_ids'] = $vouchers_id_arr;
        } else {
            $record['voucher_ids'] = [];
        }
        //判断达标
        if (!empty($record['voucher_ids'])) {
            if (in_array(1, $record['voucher_ids'])) {
                //个人订单总金额达到
                $order_total_fee = $this->order_m->personal_order_total_fee($user_id, $goods_id);
                if (intval($order_total_fee['total_fee']) >= intval($vouchers_kv[1])) {
                    $record['can_voucher_1'] = 1;
                }
            }
            if (in_array(2, $record['voucher_ids'])) {
                //个人订单数达到
                $order_total_count = $this->order_m->personal_order_total_count($user_id, $goods_id);
                if ($order_total_count >= $vouchers_kv[2]) {
                    $record['can_voucher_2'] = 1;
                }
            }
            if (in_array(3, $record['voucher_ids'])) {
                //分享点击数
                if ($share['share_click_count'] >= $vouchers_kv[3]) {
                    $record['can_voucher_3'] = 1;
                }
            }
            if (($record['can_voucher_1'] == 1) || ($record['can_voucher_2'] == 1) || ($record['can_voucher_3'] == 1) || ($record['can_voucher_4'] == 1)) {
                $can_voucher = 1;
            } else {
                $can_voucher = 0;
            }
            return $can_voucher;
        }else{
            return 0;
        }
    }



}
